var base = require('/base_info.js');
var utils = require('/utils/index.js');
Page({
  data: {
    discountList: [],
    page: 1,
    size: 10
  },
  // 获取优惠券中心列表
  getData (uid) {
    let that = this;
    // 领取优惠券中心列表
    utils.getData('SingleGetSponsorCoupon', {
      page: that.data.page,
      size: that.data.size,
      user_id: uid,
      collection_code: my.getStorageSync({
        key: 'collect', // 缓存数据的key
      }).data
    }, (res) => { 
      my.hideLoading();
      that.setData({
        discountList: that.data.discountList.concat(res.data.data),
        page: that.data.page + 1
      });
    }, (err) => {
      my.hideLoading();
      console.log(err);
    });
  },
  // 领取优惠券
  apply (e) {
    if (e.currentTarget.dataset.apply == 0 ) {
      return false;
    };
    my.showLoading();
    utils.getData('SingleReceiveCoupons', {
      coupons_id: e.currentTarget.dataset.id,
      user_id: my.getStorageSync({
        key: 'uid', // 缓存数据的key
      }).data
    }, (res) => {
      my.hideLoading();
      // 跳转至我的优惠券列表
      my.showToast({
        content: '领取成功',
        duration: 3000,
        success: () => {
          // this.getData();
        },
      });
      my.navigateTo({
        url: './discount'
      });
    }, (err) => {
      my.hideLoading();
      console.log(err);
    });
  },
  onLoad(query) {
    let that = this;
    this.setData({
      discountList: [],
      page: 1
    });
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
    let that = this;
    this.setData({
      discountList: [],
      page: 1
    });
    var uid = my.getStorageSync({
      key: 'uid', // 缓存数据的key
    }).data;
    if (!uid || uid == "") {
      utils.getAuthCode(function(res){
        that.getData(res.data.data.uid);
      });
      return false;
    }
    that.getData(uid);
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
    this.getData(my.getStorageSync({
      key: 'uid', // 缓存数据的key
    }).data);
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
});
